1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
<script lang="ts" context="module">
import { ApiClient } from "$lib/api_client";
export async function load({ params, fetch }) {
const apiClient = new ApiClient(fetch);
try {
const bot_name = params["bot_name"];
const [botData, botStats, leaderboard] = await Promise.all([
apiClient.get(`/api/bots/${bot_name}`),
apiClient.get(`/api/bots/${bot_name}/stats`),
apiClient.get("/api/leaderboard"),
]);
const { bot } = botData;
return {
props: {
bot,
botStats,
leaderboard,
},
};
} catch (error) {
return {
status: error.status,
error: error,
};
}
}
function calcMergedStats(rawStats: object) {
return Object.fromEntries(
Object.entries(rawStats).map(([opponent, ms]) => {
const mapStats = ms as { k: MatchupStats };
return [opponent, Object.values(mapStats).reduce(mergeStats)];
})
);
}
type MatchupStats = {
win: number;
tie: number;
loss: number;
};
function winRate(stats: MatchupStats) {
return (stats.win + 0.5 * stats.tie) / (stats.win + stats.tie + stats.loss);
}
function mergeStats(a: MatchupStats, b: MatchupStats): MatchupStats {
return {
win: a.win + b.win,
tie: a.tie + b.tie,
loss: a.loss + b.loss,
};
}
</script>
<script lang="ts">
export let bot: object;
export let botStats: object;
export let leaderboard: object[];
$: mergedStats = calcMergedStats(botStats);
</script>
<div class="container">
<table class="leaderboard">
<tr class="leaderboard-row leaderboard-header">
<th class="leaderboard-rank">Rank</th>
<th class="leaderboard-rating">Rating</th>
<th class="leaderboard-bot">Bot</th>
<th class="leaderboard-author">Author</th>
<th>Winrate</th>
<th>Matches</th>
</tr>
{#each leaderboard as entry, index}
<tr class="leaderboard-row">
<td class="leaderboard-rank">{index + 1}</td>
<td class="leaderboard-rating">
{entry["rating"].toFixed(0)}
</td>
<td class="leaderboard-bot">
<a class="leaderboard-href" href="/bots/{entry['bot']['name']}"
>{entry["bot"]["name"]}
</a></td
>
<td class="leaderboard-author">
{#if entry["author"]}
<!-- TODO: remove duplication -->
<a class="leaderboard-href" href="/users/{entry['author']['username']}"
>{entry["author"]["username"]}</a
>
{/if}
</td>
{#if mergedStats[entry["bot"]["name"]]}
<td>
{winRate(mergedStats[entry["bot"]["name"]]).toFixed(2)}
</td>
<td>
<a href={`/matches?bot=${bot["name"]}&opponent=${entry["bot"]["name"]}`}>view matches</a
>
</td>
{:else}
<td />
<td>no matches yet </td>{/if}
</tr>
{/each}
</table>
</div>
<style lang="scss">
.container {
width: 800px;
max-width: 80%;
margin: 50px auto;
}
.header {
display: flex;
justify-content: space-between;
align-items: flex-end;
margin-bottom: 60px;
border-bottom: 1px solid black;
}
$header-space-above-line: 12px;
.bot-name {
font-size: 24pt;
margin-bottom: $header-space-above-line;
}
.owner-name {
font-size: 14pt;
text-decoration: none;
color: #333;
margin-bottom: $header-space-above-line;
}
.leaderboard {
margin: 18px 10px;
text-align: center;
}
.leaderboard th,
.leaderboard td {
padding: 8px 16px;
}
.leaderboard-rank {
color: #333;
}
.leaderboard-href {
text-decoration: none;
color: black;
}
</style>
|